package com.shreecon.lms.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.shreecon.lms.common.EmployeeDetailsView;
import com.shreecon.lms.interfaces.IMessageConstant;
import com.shreecon.lms.interfaces.LMSException;
import com.shreecon.lms.interfaces.manager.EmployeeManagerFactory;
import com.shreecon.lms.interfaces.manager.LeaveManagerFactory;

/**
 * Servlet implementation class LeaveServlet.
 * 
 * @author Santhosh. Modified on Aug 13, 2011
 */
@SuppressWarnings("serial")
public class LeaveServlet extends HttpServlet {
       

    /* (non-Javadoc)
     * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Servlet Called");
        String empId = (String)request.getParameter("empID");
        EmployeeDetailsView detailsView = (EmployeeDetailsView)request.getSession().getAttribute("employeeDetailsView");
        String target = "/pages/ApplyLeave.jsp";
        if( detailsView == null ){
            System.out.println("Not found in session , Calling Server");
            try {
		detailsView = EmployeeManagerFactory.getInstance().getEmployeeManager().getEmployeeById(Long.valueOf(empId));
	    } catch (NumberFormatException e) {
		request.setAttribute("error", IMessageConstant.TECH_ERROR);
		target = "/pages/HomePage.jsp";
	    } catch (LMSException e) {
		request.setAttribute("error", e.getMessage());
		target = "/pages/HomePage.jsp";
	    }
            request.getSession().setAttribute("employeeDetailsView", detailsView);
        }
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(target);
        dispatcher.forward(request, response);
    }
    
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
	System.out.println("Servlet called for post");
	String fromDate = (String)request.getParameter("fromdate");
	String toDate = (String)request.getParameter("todate");
	String typeOfLeave = (String)request.getParameter("typeOfLeave");
	
	EmployeeDetailsView detailsView = (EmployeeDetailsView)request.getSession().getAttribute("employeeDetailsView");
	String target = "/pages/ApplyLeave.jsp";
	try {
	    LeaveManagerFactory.getInstance().getLeaveManager().applyLeave(fromDate, toDate,typeOfLeave, detailsView);
	} catch (LMSException e) {
	    e.printStackTrace();
	    request.setAttribute("error", e.getMessage());
	    target = "/pages/ApplyLeave.jsp";
	}
	RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(target);
        dispatcher.forward(request, response);
    }
}
